cmake_minimum_required(VERSION 3.16)

project(LearnOpenGL C CXX)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_LIBRATY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Common C/C++ Flags
if (MSVC)
	# 启用文件夹
	set_property(GLOBAL PROPERTY USE_FOLDERS ON)

	# Release Mode also generate PDB
	add_compile_options("$<$<NOT:$<CONFIG:Debug>>:/Zi>")
	add_link_options("$<$<NOT:$<CONFIG:Debug>>:/DEBUG>")
	add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:REF>")
	add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:ICF>")

	# stb_image use unicode
	add_definitions(-DSTBI_WINDOWS_UTF8)

	add_compile_options(/utf-8 /MP /Zi)
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
#	add_link_options(/NODEFAULTLIB:LIBCMTD)

	# FreeType2
	add_library(freetype2 STATIC IMPORTED)
	set_target_properties(
			freetype2
			PROPERTIES
			IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/freetype2/lib/freetype.lib
	)
	target_include_directories(freetype2 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/freetype2/include)
	#target_link_directories(freetype2 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/freetype2/lib)
	#target_link_libraries(freetype2 INTERFACE freetype$<$<CONFIG:Debug>:d>)
elseif(MINGW)

elseif(UNIX)

endif()

# third_party libraries
add_subdirectory(vendors/glfw-3.3.8 glfw-build) # GLFW 3
add_subdirectory(vendors/glm glm-build)

# source code
file(
	GLOB_RECURSE
	ALL_SRC
	src/*.*
)
if (NOT UNIX)
	list(FILTER ALL_SRC EXCLUDE REGEX ".*zview.*")
	list(FILTER ALL_SRC EXCLUDE REGEX ".*fpga.*")
endif()
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src FILES ${ALL_SRC})

add_executable(${PROJECT_NAME} ${ALL_SRC})

#if (WIN32)
#	add_executable(${PROJECT_NAME} WIN32 ${ALL_SRC})
#	if (MSVC)
#		target_link_options(${PROJECT_NAME} PRIVATE /ENTRY:mainCRTStartup)
#	endif()
#else()
#	add_executable(${PROJECT_NAME} ${ALL_SRC})
#endif()
target_link_libraries(${PROJECT_NAME} PRIVATE glfw glm::glm)
if (WIN32)
	target_link_libraries(${PROJECT_NAME} PRIVATE Shlwapi Shell32 freetype2)
else()
	find_package(PkgConfig REQUIRED)
	pkg_search_module(FT2 freetype2 REQUIRED IMPORTED_TARGET)
	target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::FT2)
endif()
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

add_custom_target(
	COPY_RESOURCES ALL
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders $<TARGET_FILE_DIR:${PROJECT_NAME}>/shaders
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/data $<TARGET_FILE_DIR:${PROJECT_NAME}>/data
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/fonts $<TARGET_FILE_DIR:${PROJECT_NAME}>/fonts
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:${PROJECT_NAME}>/assets
	COMMENT "Copy Shaders And Data ..."
	DEPENDS ${PROJECT_NAME}
)
target_precompile_headers(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/pch/proj_pch.h)

if (UNIX)
    target_include_directories(${PROJECT_NAME} PRIVATE  ${CMAKE_CURRENT_SOURCE_DIR}/src/app/zview_dev/include)
endif()